package com.seqb.manage.reptile.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lilinshen
 * @title 请填写标题
 * @description 请填写相关描述
 * @date 2020/5/9
 */
public class ImageReptileServiceImpl {

    public static Long recursion = 0L;

    public static void main(String[] args) {
        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 4,
                60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        testImageReptile(tpe, "https://www.lanvshen.com/", "");

        tpe.shutdown();
        try {
            tpe.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("所有线程执行完！");

        //路径出现中文，需要使用url编码一下
        try {
            String url = "https://pro.qcc.com/api/rpt/retrieve?compkey=80e2ebefacda11eda3f10c42a106ce72&comptoken=1DED24148F2D229925913F0BA1256D08&code=843e5ee1-6880-45f2-8ac9-f9f307c4f5b1/pdf/";
            String fileName = URLEncoder.encode("京信数据科技有限公司-企业信用决策报告-20230216153200.pdf", "utf-8");
            downloadFile(url + fileName, "d://", "1.pdf");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //爬取写真集
    public static void testImageReptile(ThreadPoolExecutor tpe, String url, String name) {
        recursion++;
        if (recursion >= 100) {
            return;
        }

        Document doc = getDocument(url);
        // 获取目标HTML代码
        Elements hezi = doc.select("div.hezi");
        for (int i = 0; i < hezi.size(); i++) {
            Element element = hezi.get(i);
            Elements li = element.select("ul>li");
            for (int j = 0; j < li.size(); j++) {
                Element element1 = li.get(j);
                Element img = element1.select("a").first().select("img").first();
                String href = img.attr("src").replace("0.jpg", "");
                Element span = element1.select("span").first();
                Long size = Long.valueOf(span.text().replace("P", ""));
                Element p = element1.select("p").eq(1).first();
                String hasName = name;
                if (StringUtils.isEmpty(hasName)) {
                    hasName = p.text().replace("模特：", "").trim();
                }
                System.out.println("链接：" + href + "，数量：" + size + "，模特：" + hasName);
                //下载图片
                tpe.execute(new DownImage(href, size, hasName));
            }
        }
        Elements hezit = doc.select("div.hezi_t");
        for (int i = 0; i < hezit.size(); i++) {
            Element element = hezit.get(i);
            Elements li = element.select("ul>li");
            for (int j = 0; j < li.size(); j++) {
                Element element1 = li.get(j);
                Element a = element1.select("a").first();
                String href = a.attr("href");
                Element p = element1.select("p").first();
                String hasName = p.select("a").first().text();
                testImageReptile(tpe, href, hasName);
            }
        }
    }

    /**
     * 下载线程
     */
    static class DownImage implements Runnable {
        String href;
        Long size;
        String name;

        public DownImage(String href, Long size, String name) {
            this.href = href;
            this.size = size;
            this.name = name;
        }

        @Override
        public void run() {
            for (int i = 0; i <= size; i++) {
                String url = href + i + ".jpg";
                String filePath = "d://image//lanvshen//" + name;
                String fileName = i + ".jpg";
                downloadFile(url, filePath, fileName);
            }
        }
    }

    //下载文件
    public static boolean downloadFile(String networkLink, String filePath, String fileName) {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        DataInputStream dataInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            URL url = new URL(networkLink);
            dataInputStream = new DataInputStream(url.openStream());
            fileOutputStream = new FileOutputStream(new File(filePath + "//" + fileName));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            int length;
            byte[] buffer = new byte[1024];
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            byte[] context = output.toByteArray();
            fileOutputStream.write(context);
            dataInputStream.close();
            fileOutputStream.close();
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                if (dataInputStream != null) {
                    dataInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return false;
    }

    //获取文档内容
    public static Document getDocument(String url) {
        try {
            //5000是设置连接超时时间，单位ms
            return Jsoup.connect(url).timeout(5000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
